package com.gpt.article.service.impl.taskHandle;

import chatgpt.ChatGPT;
import com.gpt.article.domain.GptSpiderArticle;
import com.gpt.article.domain.GptTopicTitle;
import com.gpt.article.domain.TxtClassify;
import com.gpt.article.service.impl.taskHandle.task.*;
import com.ruoyi.common.utils.DateUtils;
import com.zhucx.thread.CrawlerThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;

public class XigaoThread implements Runnable {

    private static Logger logger = LoggerFactory.getLogger(CrawlerThread.class);

    private GenTask xigaoTask;
    private ChatGPT chatGPT;

//    @Autowired
//    RunDataImpl runData;

//    private GptSpiderArticle gptSpiderArticle;
    private boolean running;
    private boolean toStop;
    private Long taskId;


    public XigaoThread(GenTask xigaoTask, ChatGPT chatGPT, Long taskId) {
        this.xigaoTask = xigaoTask;
        this.chatGPT = chatGPT;
        this.running = true;
        this.toStop = false;
        this.taskId = taskId;
    }

    public void toStop() {
        this.toStop = true;
    }
    public boolean isRunning() {
        return running;
    }
    
    
    

    
    
    @Override
    public void run() {
        String type = xigaoTask.getGentype();
        GptSpiderArticle gptSpiderArticle = null;
        GptTopicTitle gptTopicTitle = null;
        TxtClassify txtClassify = null;
        while (!toStop) {
// ------- url ----------
            try {
                running = false;
                //TODO 终止
                xigaoTask.tryFinish();

                if("title".equals(type)){
                    xigaoTask.getRunData().getGptTask();
                    running = true;
                    new GenContentTasker().genContent(chatGPT,xigaoTask,taskId);
                }else if("topic".equals(type)){
                    gptTopicTitle = xigaoTask.getRunData().getGptTask();
                    running = true;
                    new GenTopicTasker().genContent(gptTopicTitle,chatGPT,xigaoTask,taskId);
                }else if("txtClassifies".equals(type)){
                    txtClassify = xigaoTask.getRunData().getGptTask();
                    running = true;
                    new GenTxtClassifyTasker().genContent(txtClassify,chatGPT,xigaoTask,taskId);
                }else if("qaContent".equals(type)){
                    new GenQContentTasker().genContent(xigaoTask.getRunData().getGptTask(),chatGPT,xigaoTask,taskId);
                }else{
                    gptSpiderArticle = xigaoTask.getRunData().getGptTask();
                    running = true;
                    new XiGaoTasker().restContentLogic(gptSpiderArticle,chatGPT,xigaoTask,taskId);
                }


            }catch (Exception e){
//                logger.info(">>>>>>>>>>> 任务中断. {}", e.getMessage());
                if("title".equals(type)){
                    if(!ObjectUtils.isEmpty(gptSpiderArticle)){
                        gptSpiderArticle.setStatus("3");
                        gptSpiderArticle.setUpdateTime(DateUtils.getNowDate());
                        new XiGaoTasker().updateSpiderArt(gptSpiderArticle);
//                    new GptSpiderArticleServiceImpl().updateGptSpiderArticle(gptSpiderArticle);
                    }
                }
                System.out.println("异常信息：："+e.getMessage());

                new XiGaoTasker().upTask(taskId,0,0,0,"3");
            }


        }
    }
    
    
}
